/*  This file is part of the comicconquer project
    Copyright (C) 2010 Reach <reach334@gmail.com>

    ComicConquer is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This software is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public 
    License along with this software.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "KeyPage.h"

extern CoListItems g_mainOperations;

CoListItems g_keys;

// key page
KeyPage::KeyPage(CoKeyMap& keyMap, QWidget* parent)
: CoTabPage(parent)
{
    In_Log(__FUNCTION__, COCLASSNAME);

    m_keyMap = &keyMap;

    g_keys.clear();
    g_keys.append(CoListItem(tr("MENU"), Qt::Key_Menu));
    g_keys.append(CoListItem(tr("BACK"), Qt::Key_Escape));
    g_keys.append(CoListItem(tr("PREV"), Qt::Key_PageUp));
    g_keys.append(CoListItem(tr("NEXT"), Qt::Key_PageDown));
    g_keys.append(CoListItem(tr("OK"), Qt::Key_Return));
    g_keys.append(CoListItem(tr("Up"), Qt::Key_Up));
    g_keys.append(CoListItem(tr("Down"), Qt::Key_Down));
    g_keys.append(CoListItem(tr("Left"), Qt::Key_Left));
    g_keys.append(CoListItem(tr("Right"), Qt::Key_Right));

    Out_Log(__FUNCTION__, COCLASSNAME);
}

void KeyPage::initByKeyMap()
{
    In_Log(__FUNCTION__, COCLASSNAME);

    clear();

    for (int i = 0; i < g_keys.count(); i++)
    {
        KeyViewWidget* viewColumn = new KeyViewWidget(g_keys[i].str, &g_mainOperations[m_keyMap->value(g_keys[i].id)], this);
        addWidget(QString::number(i), viewColumn);
    }

    Out_Log(__FUNCTION__, COCLASSNAME);
}

void KeyPage::writeToKeyMap()
{
    In_Log(__FUNCTION__, COCLASSNAME);

    for (int i = 0; i < g_keys.count(); i++)
    {
        if (m_keyMap->contains(g_keys[i].id))
        {
            (*m_keyMap)[g_keys[i].id] = static_cast<KeyViewWidget*>(getWidget(i))->m_operItem->id;
        }
    }

    Out_Log(__FUNCTION__, COCLASSNAME);
}

